package com.ruoyi.web.controller.api;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.RedisConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.redis.RedisCacheUtil;
import com.ruoyi.xsp.domain.*;
import com.ruoyi.xsp.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户管理 api
 *
 * @author zhiminhu
 * @date 2021-01-22
 */
@Api(value = "视频信息管理", tags = "视频信息管理")
@RestController
@RequestMapping("/api/news")
public class XspNewsApiController extends BaseApiController {
    private static final Logger log = LoggerFactory.getLogger(XspUserApiController.class);
    @Autowired
    private IXspNewsInfoService xspNewsInfoService;
    @Autowired
    private RedisCacheUtil redisCache;

    @Autowired
    private IXspLikeService xspLikeService;
    @Autowired
    private IXspUserConcernedService xspUserConcernedService;
    @Autowired
    private IXspUserService xspUserService;

    @Autowired
    private IXspCommentService xspCommentService;

    @Autowired
    private IXspCommentReplyService xspCommentReplyService;

    /**
     * 查询视频列表
     */
    @ApiOperation("视频列表")
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list(HttpServletRequest request, XspNewsInfo xspNewsInfo) {
        if (xspNewsInfo.getType() == null) {
            xspNewsInfo.setType(1);
        }
        xspNewsInfo.setIsShow(1);
        xspNewsInfo.setDelFlag(0);
        if (!StringUtils.isBlank(ServletUtils.getParameter(Constants.PAGE_SIZE_API))) {
            startPage();
        } else {
            PageHelper.startPage(0, 10, "news_id");
        }
        List<XspNewsInfo> list = xspNewsInfoService.selectXspNewsInfoList(xspNewsInfo);
        return AjaxResult.success(getDataTable(list));
    }
    /**
     * 基于定位的视频列表
     */
    @ApiOperation("基于定位的视频列表")
    @GetMapping("/lbsList")
    @ResponseBody
    public AjaxResult lbsList(HttpServletRequest request, XspNewsInfo xspNewsInfo) {
        xspNewsInfo.setType(1);
        xspNewsInfo.setIsShow(1);
        xspNewsInfo.setDelFlag(0);

        if (!StringUtils.isBlank(ServletUtils.getParameter(Constants.PAGE_SIZE_API))) {
            startPage();
        } else {
            PageHelper.startPage(1, 10, "news_id");
        }
        List<XspNewsInfo> list = xspNewsInfoService.selectXspNewsInfoList(xspNewsInfo);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 热点/亮点视频列表"
     */
    @ApiOperation("热点/亮点/发现视频列表")
    @GetMapping("/hotList")
    @ResponseBody
    public AjaxResult hotList(HttpServletRequest request, XspNewsInfo xspNewsInfo) {
        xspNewsInfo.setType(1);
        xspNewsInfo.setIsShow(1);
        xspNewsInfo.setDelFlag(0);
        xspNewsInfo.setTag("美女");
        PageHelper.startPage(1, 2, " like_count,comment_count desc");
        List<XspNewsInfo> list1 = xspNewsInfoService.selectXspNewsInfoList(xspNewsInfo);
        xspNewsInfo.setTag("小姐");
        PageHelper.startPage(1, 4, " like_count,comment_count desc");
        List<XspNewsInfo> list2 = xspNewsInfoService.selectXspNewsInfoList(xspNewsInfo);
        xspNewsInfo.setTag("女骑");
        PageHelper.startPage(1, 10, " like_count,comment_count desc");
        List<XspNewsInfo> list3 = xspNewsInfoService.selectXspNewsInfoList(xspNewsInfo);
        Map<String,Object> map=new HashMap<>();
        map.put("list1",list1);
        map.put("list2",list2);
        map.put("list3",list3);
        map.put("title1","时尚美女");
        map.put("title2","漂亮小姐姐");
        map.put("title3","女骑士");
        return AjaxResult.success(map);
    }

    /**
     * 热点/亮点 获取更多的数据列表"
     */
    @ApiOperation("热点/亮点视频列表")
    @GetMapping("/hotListAll")
    @ResponseBody
    public AjaxResult hotListAll(HttpServletRequest request, XspNewsInfo xspNewsInfo) {
        xspNewsInfo.setType(1);
        xspNewsInfo.setIsShow(1);
        xspNewsInfo.setDelFlag(0);
        if(StringUtils.isBlank(xspNewsInfo.getTag())){
            xspNewsInfo.setTag("女骑");
        }
        if (!StringUtils.isBlank(ServletUtils.getParameter(Constants.PAGE_SIZE_API))) {
            startPage();
        } else {
            PageHelper.startPage(1, 10, "news_id");
        }
        List<XspNewsInfo> list = xspNewsInfoService.selectXspNewsInfoList(xspNewsInfo);
        return AjaxResult.success(getDataTable(list));
    }


    /**
     * 发现模块视频列表"
     */
    @ApiOperation("发现模块视频列表")
    @GetMapping("/findList")
    @ResponseBody
    public AjaxResult findList(HttpServletRequest request, XspNewsInfo xspNewsInfo) {
        xspNewsInfo.setType(1);
        xspNewsInfo.setIsShow(1);
        xspNewsInfo.setDelFlag(0);
        xspNewsInfo.setTag("女人");
        PageHelper.startPage(1, 4, " like_count,comment_count desc");
        List<XspNewsInfo> list1 = xspNewsInfoService.selectXspNewsInfoList(xspNewsInfo);
        xspNewsInfo.setTag("男人");
        PageHelper.startPage(1, 4, " like_count,comment_count desc");
        List<XspNewsInfo> list2 = xspNewsInfoService.selectXspNewsInfoList(xspNewsInfo);
        xspNewsInfo.setTag("发现");
        PageHelper.startPage(1, 10, " like_count,comment_count desc");
        List<XspNewsInfo> list3 = xspNewsInfoService.selectXspNewsInfoList(xspNewsInfo);
        Map<String,Object> map=new HashMap<>();
        map.put("list1",list1);

        map.put("list2",list2);
        map.put("list3",list3);
        map.put("title1","现代女性");
        map.put("title2","男人帮");
        map.put("title3","发现精彩");
        return AjaxResult.success(map);
    }

    /**
     * 发现模块视频列表  获取更多的数据列表"
     */
    @ApiOperation("发现模块视频列表")
    @GetMapping("/findListAll")
    @ResponseBody
    public AjaxResult findListAll(HttpServletRequest request, XspNewsInfo xspNewsInfo) {
        xspNewsInfo.setType(1);
        xspNewsInfo.setIsShow(1);
        xspNewsInfo.setDelFlag(0);
        if(StringUtils.isBlank(xspNewsInfo.getTag())){
            xspNewsInfo.setTag("发现");
        }
        if (!StringUtils.isBlank(ServletUtils.getParameter(Constants.PAGE_SIZE_API))) {
            startPage();
        } else {
            PageHelper.startPage(1, 10, "news_id");
        }
        List<XspNewsInfo> list = xspNewsInfoService.selectXspNewsInfoList(xspNewsInfo);
        return AjaxResult.success(getDataTable(list));
    }


    /**
     * 查询我关注的用户的视频列表
     */
    @ApiOperation("关注的用户视频列表")
    @GetMapping("/concernedList")
    @ResponseBody
    public AjaxResult concernedList(HttpServletRequest request, XspNewsInfo xspNewsInfo) {
        if (xspNewsInfo.getType() == null) {
            xspNewsInfo.setType(1);
        }
        xspNewsInfo.setIsShow(1);
        xspNewsInfo.setDelFlag(0);

        List<XspNewsInfo> list;
        String token = request.getHeader("TAP-CLIENT-TOKEN");
        if (token == null) {
            return AjaxResult.error("登陆信息不能为空，请登录后在操作！");
        }
        Long userCode= redisCache.getCacheObject(RedisConstants.TOKEN_USER_PREFIX+token);
        if(userCode==null || userCode<=0){
            return AjaxResult.error("登陆信息已经失效，请重新登陆！");
        }
        XspUser xspUser= redisCache.getCacheObject(RedisConstants.USER_INFO_PREFIX+userCode);
        if(xspUser==null) {
            //缓存失效 从新获取
            xspUser=xspUserService.selectXspUserForCacheByUserCode(Long.valueOf(userCode));
            //如果没有登录或者没有关注，显示推荐视频
            startPage();
            list = xspNewsInfoService.selectXspNewsInfoList(xspNewsInfo);
            return AjaxResult.success(getDataTable(list));
        }

        xspNewsInfo.setUserCode(xspUser.getUserCode());
        startPage();
        list = xspNewsInfoService.selectXspNewsInfoListByMyConcerned(xspNewsInfo);
        if (list.size() == 0) {
            //如果没有登录或者没有关注，显示推荐视频
            xspNewsInfo.setUserCode(null);
            startPage();
            list = xspNewsInfoService.selectXspNewsInfoList(xspNewsInfo);
        }
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 查询我的视频列表
     */
    @ApiOperation("查询我的视频列表")
    @GetMapping("/myNewsList")
    @ResponseBody
    public AjaxResult myNewsList(HttpServletRequest request) {
        String token = request.getHeader("TAP-CLIENT-TOKEN");
        if (token == null) {
            return AjaxResult.error("登陆信息不能为空，请登录后在操作！");
        }
        Long userCode= redisCache.getCacheObject(RedisConstants.TOKEN_USER_PREFIX+token);
        if(userCode==null || userCode<=0){
            return AjaxResult.error("登陆信息已经失效，请重新登陆！");
        }
        XspUser xspUser= redisCache.getCacheObject(RedisConstants.USER_INFO_PREFIX+userCode);
        if(xspUser==null) {
            //缓存失效 从新获取
            xspUser=xspUserService.selectXspUserForCacheByUserCode(Long.valueOf(userCode));
        }
        XspNewsInfo xspNewsInfo=new XspNewsInfo();
        xspNewsInfo.setType(1);
        xspNewsInfo.setIsShow(1);
        xspNewsInfo.setDelFlag(0);
        xspNewsInfo.setUserCode(xspUser.getUserCode());
        if (!StringUtils.isBlank(ServletUtils.getParameter(Constants.PAGE_SIZE_API))) {
            startPage();
        } else {
            PageHelper.startPage(1, 10);
        }
        List<XspNewsInfo> list= xspNewsInfoService.selectXspNewsInfoList(xspNewsInfo);

        return AjaxResult.success(getDataTable(list));
    }
    /**
     * 查询我喜欢的视频列表
     */
    @ApiOperation("我喜欢的视频列表")
    @GetMapping("/getMyLikeList")
    @ResponseBody
    public AjaxResult getMyLikeList(HttpServletRequest request) {
        String token = request.getHeader("TAP-CLIENT-TOKEN");
        if (token == null) {
            return AjaxResult.error("登陆信息不能为空，请登录后在操作！");
        }
        Long userCode= redisCache.getCacheObject(RedisConstants.TOKEN_USER_PREFIX+token);
        if(userCode==null || userCode<=0){
            return AjaxResult.error("登陆信息已经失效，请重新登陆！");
        }
        XspUser xspUser= redisCache.getCacheObject(RedisConstants.USER_INFO_PREFIX+userCode);
        if(xspUser==null) {
            //缓存失效 从新获取
            xspUser=xspUserService.selectXspUserForCacheByUserCode(Long.valueOf(userCode));
        }
        XspNewsInfo xspNewsInfo=new XspNewsInfo();
        xspNewsInfo.setType(1);
        xspNewsInfo.setIsShow(1);
        xspNewsInfo.setDelFlag(0);
        xspNewsInfo.setUserCode(xspUser.getUserCode());
        if (!StringUtils.isBlank(ServletUtils.getParameter(Constants.PAGE_SIZE_API))) {
            startPage();
        } else {
            PageHelper.startPage(1, 10, "create_time");
        }
        List<XspNewsInfo> list= xspNewsInfoService.selectXspNewsInfoListByMyLike(xspNewsInfo);

        return AjaxResult.success(getDataTable(list));
    }
    /**
     * 查询视频详细
     */
    @ApiOperation("视频详细")
    @GetMapping("/getNewsInfo")
    @ResponseBody
    public AjaxResult getNewsInfo(HttpServletRequest request,Long newsId) {
        if (newsId == null) {
            return AjaxResult.error("内容ID参数不能为空");
        }
        XspNewsInfo newsInfo = xspNewsInfoService.selectXspNewsInfoById(newsId);
        if (newsInfo == null) {
            return AjaxResult.error("视频不存在");
        }
        String token = request.getHeader("TAP-CLIENT-TOKEN");
        if (token != null) {
            Long userCode= redisCache.getCacheObject(RedisConstants.TOKEN_USER_PREFIX+token);
            if(userCode>0){
                XspUser xspUser = redisCache.getCacheObject(RedisConstants.USER_INFO_PREFIX + userCode);
                if (xspUser == null) {
                    //缓存失效 从新获取
                    xspUser = xspUserService.selectXspUserForCacheByUserCode(Long.valueOf(userCode));
                }
                XspLike like=new XspLike();
                like.setLikeId(newsInfo.getNewsId());
                like.setUserCode(xspUser.getUserCode());
                XspUserConcerned userConcerned=new XspUserConcerned();
                userConcerned.setFanUserCode(xspUser.getUserCode());
                userConcerned.setToUserCode(newsInfo.getUserCode());
                xspUser.setIConcernedIt(xspUserConcernedService.selectXspUserConcernedListCount(userConcerned)>0);
                newsInfo.setILikeIt(xspLikeService.selectXspLikeListCount(like)>0);
            }
        }


        return AjaxResult.success(newsInfo);
    }

    /**
     * 查询视频评论
     */
    @ApiOperation("查询视频评论")
    @GetMapping("/getNewsComment")
    @ResponseBody
    public AjaxResult getNewsComment(Long newsId) {

        XspNewsInfo newsInfo = xspNewsInfoService.selectXspNewsInfoById(newsId);
        if (newsInfo == null) {
            return AjaxResult.error("视频不存在");
        }
        XspComment comment = new XspComment();
        comment.setTopicId(newsInfo.getNewsId().intValue());
        comment.setStatus(1);
        if (!StringUtils.isBlank(ServletUtils.getParameter(Constants.PAGE_SIZE_API))) {
            startPage();
        } else {
            PageHelper.startPage(0, 10, "is_top desc,is_hot desc,create_time desc");
        }
        List<XspComment> comments = xspCommentService.selectXspCommentList(comment);
//        for (XspComment xspComment : comments){
//            if()
//        }
        return AjaxResult.success(getDataTable(comments));
    }

    /**
     * 删除视频评论
     */
    @ApiOperation("删除视频评论")
    @PostMapping("/delNewsComment")
    @ResponseBody
    public AjaxResult delNewsComment(HttpServletRequest request, Integer commentId) {
        String token = request.getHeader("TAP-CLIENT-TOKEN");
        if (token == null) {
            return AjaxResult.error("登陆信息不能为空，请登录后在操作！");
        }
        Long userCode= redisCache.getCacheObject(RedisConstants.TOKEN_USER_PREFIX+token);
        if(userCode==null || userCode<=0){
            return AjaxResult.error("登陆信息已经失效，请重新登陆！");
        }
        XspUser xspUser= redisCache.getCacheObject(RedisConstants.USER_INFO_PREFIX+userCode);
        if(xspUser==null) {
            //缓存失效 从新获取
            xspUser=xspUserService.selectXspUserForCacheByUserCode(Long.valueOf(userCode));
        }
        XspComment xspComment = xspCommentService.selectXspCommentById(commentId);
        if (xspComment == null) {
            return AjaxResult.error("评论不存在");
        }
        if (!xspUser.getUserCode().equals(xspComment.getFromUid())) {
            return AjaxResult.error("不允许删除其他人的评论");
        }
        if (xspCommentService.deleteXspCommentById(commentId) > 0) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 查询视频评论回复
     */
    @ApiOperation("查询视频评论回复")
    @GetMapping("/getNewsCommentReply")
    @ResponseBody
    public AjaxResult getNewsCommentReply(Integer commentId) {

        XspComment xspComment = xspCommentService.selectXspCommentById(commentId);
        if (xspComment == null) {
            return AjaxResult.error("评论不存在");
        }
        XspCommentReply reply = new XspCommentReply();
        reply.setCommentId(xspComment.getId());
        reply.setStatus(1);
        if (!StringUtils.isBlank(ServletUtils.getParameter(Constants.PAGE_SIZE_API))) {
            startPage();
        } else {
            PageHelper.startPage(0, 10, "create_time desc");
        }
        List<XspCommentReply> replys = xspCommentReplyService.selectXspCommentReplyList(reply);
        return AjaxResult.success(getDataTable(replys));
    }

    /**
     * 删除视频评论的回复
     */
    @ApiOperation("删除视频评论的回复")
    @PostMapping("/delNewsCommentReply")
    @ResponseBody
    public AjaxResult delNewsCommentReply(HttpServletRequest request, Integer replyId) {
        String token = request.getHeader("TAP-CLIENT-TOKEN");
        if (token == null) {
            return AjaxResult.error("登陆信息不能为空，请登录后在操作！");
        }
        Long userCode= redisCache.getCacheObject(RedisConstants.TOKEN_USER_PREFIX+token);
        if(userCode==null || userCode<=0){
            return AjaxResult.error("登陆信息已经失效，请重新登陆！");
        }
        XspUser xspUser= redisCache.getCacheObject(RedisConstants.USER_INFO_PREFIX+userCode);
        if(xspUser==null) {
            //缓存失效 从新获取
            xspUser=xspUserService.selectXspUserForCacheByUserCode(Long.valueOf(userCode));
        }
        XspCommentReply xspComment = xspCommentReplyService.selectXspCommentReplyById(replyId);
        if (xspComment == null) {
            return AjaxResult.error("回复不存在");
        }
        if (!xspUser.getUserCode().equals(xspComment.getFromUid())) {
            return AjaxResult.error("不允许删除其他人的回复");
        }
        if (xspCommentReplyService.deleteXspCommentReplyById(replyId) > 0) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败");
        }
    }

    @ApiOperation("视频发布")
    @PostMapping("/saveNewsInfo")
    @ResponseBody
    public AjaxResult addSave(HttpServletRequest request, XspNewsInfo xspNewsInfo) {
        String token = request.getHeader("TAP-CLIENT-TOKEN");
        if (token == null) {
            return AjaxResult.error("token不能为空");
        }
        Long userCode= redisCache.getCacheObject(RedisConstants.TOKEN_USER_PREFIX+token);
        if(userCode==null || userCode<=0){
            return AjaxResult.error("登陆信息已经失效，请重新登陆！");
        }
        XspUser xspUser= redisCache.getCacheObject(RedisConstants.USER_INFO_PREFIX+userCode);
        if(xspUser==null) {
            //缓存失效 从新获取
            xspUser=xspUserService.selectXspUserForCacheByUserCode(Long.valueOf(userCode));
        }
        if (StringUtils.isBlank(xspNewsInfo.getTitle())) {
            return AjaxResult.error("标题不能为空");
        }
        if (StringUtils.isBlank(xspNewsInfo.getPictureAddr())) {
            return AjaxResult.error("不能为空");
        }
        if (StringUtils.isBlank(xspNewsInfo.getVideoAddr())) {
            return AjaxResult.error("视频地址不能为空");
        }
        if (xspNewsInfo.getNewsTypeId() == 0) {
            return AjaxResult.error("视频类型不能为空");
        }
        xspNewsInfo.setAvatar(xspUser.getAvatar());
        xspNewsInfo.setCreateBy(xspUser.getNickname());
        xspNewsInfo.setIsShow(0);
        xspNewsInfo.setDelFlag(0);
        xspNewsInfo.setNickname(xspUser.getNickname());
        xspNewsInfo.setUserCode(xspUser.getUserCode());
        xspNewsInfo.setCreateTime(new Date());
        return toAjax(xspNewsInfoService.insertXspNewsInfo(xspNewsInfo));
    }

    /***
     * 点赞/取消点赞 视频
     * @param request
     * @param newsId
     * @param type 0 点赞（默认） 1 取消点赞
     * @return
     */
    @ApiOperation("点赞视频")
    @PostMapping("/like")
    @ResponseBody
    public AjaxResult like(HttpServletRequest request, @Param("newsId") Long newsId, @Param("type") Integer type) {
        if (newsId == null || newsId == 0) {
            return AjaxResult.error("点赞目标不能为空");
        }
        String token = request.getHeader("TAP-CLIENT-TOKEN");
        if (token == null) {
            return AjaxResult.error("token不能为空");
        }
        Long userCode= redisCache.getCacheObject(RedisConstants.TOKEN_USER_PREFIX+token);
        if(userCode==null || userCode<=0){
            return AjaxResult.error("登陆信息已经失效，请重新登陆！");
        }
        XspUser xspUser= redisCache.getCacheObject(RedisConstants.USER_INFO_PREFIX+userCode);
        if(xspUser==null) {
            //缓存失效 从新获取
            xspUser=xspUserService.selectXspUserForCacheByUserCode(Long.valueOf(userCode));
        }
        XspNewsInfo newsInfo = xspNewsInfoService.selectXspNewsInfoById(Long.valueOf(newsId));
        if (newsInfo == null) {
            return AjaxResult.error("评论ID不存在");
        }
        XspLike like = new XspLike();
        like.setUserCode(xspUser.getUserCode());
        like.setLikeId(newsId);
        like.setType(2);
        like.setCreateBy("");
        like.setCreateTime(new Date());
        if (type == null || type == 0) {
            xspLikeService.insertXspLike(like);
            newsInfo.setLikeCount(newsInfo.getLikeCount() + 1);
            xspNewsInfoService.updateXspNewsInfo(newsInfo);
            return AjaxResult.success("点赞成功");
        } else {
            //取消点赞
            newsInfo.setLikeCount(newsInfo.getLikeCount() - 1);
            xspNewsInfoService.updateXspNewsInfo(newsInfo);
            xspLikeService.deleteLikeByUserOrId(like);
            return AjaxResult.success("取消成功");
        }

        //return  AjaxResult.success("操作成功");
    }

    /**
     * 评论内容
     */
    @ApiOperation("评论内容")
    @PostMapping("/comment")
    @ResponseBody
    public AjaxResult comment(HttpServletRequest request, @Param("newsId") String newsId, @Param("content") String content) {
        if (StringUtils.isBlank(newsId)) {
            return AjaxResult.error("评论目标不能为空");
        }
        if (StringUtils.isBlank(content)) {
            return AjaxResult.error("评论内容不能为空");
        }
        String token = request.getHeader("TAP-CLIENT-TOKEN");
        if (token == null) {
            return AjaxResult.error("token不能为空");
        }
        Long userCode= redisCache.getCacheObject(RedisConstants.TOKEN_USER_PREFIX+token);
        if(userCode==null || userCode<=0){
            return AjaxResult.error("登陆信息已经失效，请重新登陆！");
        }
        XspUser xspUser= redisCache.getCacheObject(RedisConstants.USER_INFO_PREFIX+userCode);
        if(xspUser==null) {
            //缓存失效 从新获取
            xspUser=xspUserService.selectXspUserForCacheByUserCode(Long.valueOf(userCode));
        }

        XspNewsInfo newsInfo = xspNewsInfoService.selectXspNewsInfoById(Long.valueOf(newsId));
        if (newsInfo == null) {
            return AjaxResult.error("评论ID不存在");
        }
        XspComment comment = new XspComment();
        comment.setTopicId(Integer.parseInt(newsId));
        comment.setTopicType(0);
        comment.setContent(content);
        comment.setFromUid(xspUser.getUserCode());
        comment.setNickname(xspUser.getNickname());
        comment.setThumbImg(xspUser.getAvatar());
        comment.setIsTop(0);
        comment.setIsHot(0);
        comment.setLikeNum(0);
        comment.setReplyNum(0);
        comment.setIsReply(0);
        comment.setStatus(0);
        comment.setCreateBy("");
        comment.setCreateTime(new Date());
        comment.setUpdateBy("");
        comment.setUpdateTime(new Date());
        xspCommentService.insertXspComment(comment);
        return AjaxResult.success("操作成功");
    }

    /**
     * 回复评论
     */
    @ApiOperation("回复评论")
    @PostMapping("/replay")
    @ResponseBody
    public AjaxResult replay(HttpServletRequest request, @Param("commentId") String commentId, @Param("replyId") String replyId, @Param("content") String content) {
        if (StringUtils.isBlank(commentId)) {
            return AjaxResult.error("回复目标不能为空");
        }
        if (StringUtils.isBlank(content)) {
            return AjaxResult.error("回复内容不能为空");
        }
        String token = request.getHeader("TAP-CLIENT-TOKEN");
        if (token == null) {
            return AjaxResult.error("登陆信息不能为空，请登录后在操作！");
        }
        Long userCode= redisCache.getCacheObject(RedisConstants.TOKEN_USER_PREFIX+token);
        if(userCode==null || userCode<=0){
            return AjaxResult.error("登陆信息已经失效，请重新登陆！");
        }
        XspUser xspUser= redisCache.getCacheObject(RedisConstants.USER_INFO_PREFIX+userCode);
        if(xspUser==null) {
            //缓存失效 从新获取
            xspUser=xspUserService.selectXspUserForCacheByUserCode(Long.valueOf(userCode));
        }
        XspComment xspComment = xspCommentService.selectXspCommentById(Integer.parseInt(commentId));
        if (xspComment == null) {
            return AjaxResult.error("评论ID不存在");
        }
        XspCommentReply reply = new XspCommentReply();
        reply.setCommentId(xspComment.getId());
        reply.setContent(content);

        reply.setFromUid(xspUser.getUserCode());
        reply.setFromThumbImg(xspUser.getAvatar());
        reply.setFromNickname(xspUser.getNickname());
        reply.setIsAuthor(0);
        reply.setStatus(0);
        reply.setCreateBy("");
        reply.setCreateTime(new Date());
        reply.setUpdateBy("");
        reply.setUpdateTime(new Date());


        if (StringUtils.isBlank(replyId)) {
            //回复评论
            reply.setReplyId(null);
            reply.setReplyType(1);
        } else {
            //回复别人的回复
            XspCommentReply reply1 = xspCommentReplyService.selectXspCommentReplyById(Integer.parseInt(replyId));
            if (reply1 == null) {
                return AjaxResult.error("回复记录不存在");
            }
            if (reply1.getCommentId().equals(xspComment.getId())) {
                return AjaxResult.error("回复记录的目标ID不一致");
            }
            reply.setToUid(reply1.getFromUid());
            reply.setToNickname(reply1.getFromNickname());
            reply.setReplyId(Integer.parseInt(replyId));
            reply.setReplyType(2);
        }
        xspCommentReplyService.insertXspCommentReply(reply);
        xspComment.setIsReply(1);
        //审核的时候在加数量
        //xspComment.setReplyNum(xspComment.getReplyNum()+1);
        xspCommentService.updateXspComment(xspComment);
        return AjaxResult.success("操作成功");
    }



}
